package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IStrateCommentService;
import cn.wolfcode.trip.base.service.IStrategyLikeService;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrateCommentService strateCommentService;
    @Autowired
    private IStrategyLikeService strategyLikeService;


    @GetMapping
    public Object getStrateiesByState(@ModelAttribute("qo") StrategyQueryObject qo) {
        return strategyService.selectHot(qo);
    }

    @GetMapping("/{strategyId}/travels")
    public Object getTravelsByStrategyId(@PathVariable("strategyId") Long strategyId) {
        return strategyService.getTravelsByStrategyId(strategyId);
    }

    @GetMapping("/{strategyId}/catalogs")
    public Object getCatalogByStrategyId(@PathVariable("strategyId") Long strategyId) {
        return strategyService.getCatalogByStrategyId(strategyId);
    }

    @GetMapping("/{strategyId}")
    public Object getStrategyById(@PathVariable("strategyId") Long strategyId) {
        return strategyService.get(strategyId);
    }
    // 发送请求获取当前攻略所有的标签

    /**
     * 接口分析:
     * 1.确定资源: /strategies/{strategyId}/tags
     * 2.请求方式:GET
     * 3.请求参数:strategyId
     * 4.返回结果:List<Tag> tags JSON
     */
    @GetMapping("/{strategyId}/tags")
    public Object queryTagsByStrategyId(@PathVariable("strategyId") Long strategyId) {
        return strategyService.queryTagsByStrategyId(strategyId);
    }

    //保存攻略点评
    @PostMapping("/{strategyId}/comments")
    public Object saveStrategyComment(@PathVariable("strategyId") Long strategyId, StrategyComment strategyComment) {
        Strategy strategy = new Strategy();
        strategy.setId(strategyId);
        strategyComment.setStrategy(strategy);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            strategyComment.setUser(UserContext.getCurrentUser());
            Long strategyCommentId = strateCommentService.save(strategyComment);
            map.put("id", strategyCommentId);
            map.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("msg", e.getMessage());
        }
        return map;
    }

    // ====================刘涛==================================
    // 攻略点赞开发判断当前用户是否登录
    @GetMapping("/isLogin")
    public Object isLogin() {
        Map<String, Object> map = new HashMap<String, Object>(); // 用map来封装需求数据
        User currentUser = UserContext.getCurrentUser();
        if (currentUser == null) {
            map.put("state", 0);
        } else {
            map.put("state", 1);
        }
        return map;
    }

    // 攻略点赞
    @PostMapping("like")
    public Object getCountAndState(Long strategyId) {
        // 获取系统当前登录用户
        Long userId = UserContext.getCurrentUser().getId();
        // 获取点赞数(返回值包括state用于店点赞状态的显示)
        Long num = strategyLikeService.checkNumByUserIdAndStrategyId(userId, strategyId);
        return num;
    }

    // 获取当前攻略点赞数
    @GetMapping("/{strategyId}/num")
    public Object getNumByStrategyId(@PathVariable Long strategyId) {
        return strategyLikeService.getNumByStrategyId(strategyId);
    }

    // =====================end==================================

    // ======================wongyk==============================

    /**
     * 查询攻略评论
     *
     * @param qo 分页查询条件
     * @return 分页数据
     */
    @GetMapping("comments")
    public Object getStrategyComment(StrategyCommentQueryObject qo) {
        try {
            PageInfo pageInfo = strateCommentService.query(qo);
            return pageInfo;
        } catch (Exception e) {
            return null;
        }
    }

    // =====================end==================================
}
